package org.example.completed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.completed.domain.User;
import org.example.completed.mapper.UserMapper;
import org.example.completed.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override   // 保存用户
    public void saveUser(User u) {
        u.setIsDelete(0);
        userMapper.insertUser(u);
    }

    @Override   // 搜索用户
    public User selectUser(User u) {
        if (u.getUsername() == null || u.getPassword() == null) {
            return null;
        }
        return userMapper.selectOneUser(u.getUsername(),u.getPassword());
    }

    @Override
    public List<User> findAll() {
        // 从数据库查询所有用户
        return userMapper.selectAllUsers();
    }

    @Override
    public void updateUserRights(String username, String rights) {
        userMapper.updateUserRights(username, rights); // 调用Mapper更新用户权益
    }

    //按照username查询img头像
    public String getImgByUsername(String username){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getUsername,username);
        User user = userMapper.selectOne(wrapper);
        return user.getImg();
    }
}
